<template>
  <div>
    <el-dialog v-model="dialogVisible" title="新增家属" width="500" :before-close="handleClose">
      <span>
        <!-- 表单 -->
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
          status-icon
        >
          <el-form-item label="姓名：" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入姓名" clearable />
          </el-form-item>
          <el-form-item label="联系电话：" prop="mobile">
            <el-input v-model="ruleForm.mobile" placeholder="请输入联系电话" clearable />
          </el-form-item>
          <el-form-item label="身份证号：" prop="idCard">
            <el-input v-model="ruleForm.idCard" placeholder="请输入身份证号" clearable />
          </el-form-item>
          <el-form-item label="与老人关系：" prop="relation">
            <el-select v-model="ruleForm.relation" placeholder="请选择与老人关系" clearable>
              <!-- <el-option v-for="item in relationList" :label=item :value=item /> -->
            </el-select>
          </el-form-item>
          <el-form-item label="性别：" prop="gender">
            <el-radio-group v-model="ruleForm.gender">
              <el-radio :value="1">男</el-radio>
              <el-radio :value="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="联系地址：" prop="address">
            <el-input v-model="ruleForm.address" placeholder="请输入联系地址" clearable />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, defineEmits, defineProps } from "vue";
import { FormInstance, FormRules } from "element-plus";
import { FamilyData } from "@/api/market/customer/type";

const ruleFormRef = ref<FormInstance>();

const props = defineProps({
  family: Object,
});

// console.log("props", props.family);

const emit = defineEmits(["close"]);

//dialog默认显示
const dialogVisible = ref(true);

//关闭方法
const handleClose = () => {
  emit("close");
};

//关系
const relationList = reactive(["父子关系", "父女关系", "母子关系", "母女关系", "其他"]);

//添加家属信息
const ruleForm = reactive<FamilyData>({
  id: 0,
  name: "",
  mobile: "",
  idCard: "",
  relation: "",
  gender: 0,
  address: "",
});

//规则
const rules = reactive<FormRules<FamilyData>>({
  name: [{ required: true, message: "请输入老人姓名", trigger: "blur" }],
  mobile: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
  idCard: [{ required: true, message: "请输入身份证号", trigger: "blur" }],
  relation: [
    {
      required: true,
      message: "请选择与老人关系",
      trigger: "change",
    },
  ],
  gender: [
    {
      required: true,
      message: "请选择性别",
      trigger: "change",
    },
  ],
  address: [{ required: true, message: "请输入联系地址", trigger: "blur" }],
});

//确定
const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      emit("close", ruleForm);
    } else {
      ElMessage({
        message: "error submit!",
        type: "warning",
      });
    }
  });
};

onMounted(() => {
  if (props.family) {
    Object.assign(ruleForm, props.family);
  }
});
</script>

<style scoped></style>
